﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LotteryVote.Entity;
using LotteryVote.Utility;
using LotteryVote.Data.Drop;
using LotteryVote.Logic.Limit;
using LotteryVote.Logic.Exceptions;

namespace LotteryVote.Logic
{
    public class BetUpperLimitFacade
    {
        private BetUpperLimitDataAccess _daUpperLimit;
        internal BetUpperLimitDataAccess DaUpperLimit
        {
            get
            {
                if (_daUpperLimit == null)
                    _daUpperLimit = new BetUpperLimitDataAccess();
                return _daUpperLimit;
            }
        }

        /// <summary>
        /// 增加限制金额.
        /// </summary>
        /// <param name="limitId">The limit id.</param>
        /// <param name="increaseAmount">The increase amount.</param>
        public void IncreaseLimit(int limitId, decimal increaseAmount)
        {
            var limit = LimitContext.Instance.CacheUpperLimitList.Find(it => it.LimitId == limitId);
            if (limit == null)
                throw new PromptException("Specified does not exist ceilings!");
            limit.UpperLlimit += increaseAmount;
            DaUpperLimit.Update(limit);
            LimitContext.Instance.UpdateUpperLimitToDB(limit);
        }
        public void UpdateUpperLimit(int limitId, decimal amount)
        {
            var limit = LimitContext.Instance.CacheUpperLimitList.Find(it => it.LimitId == limitId);
            if (limit == null)
            {
                limit = DaUpperLimit.GetUpperLimit(limitId);
                if (limit == null)
                    throw new PromptException("不存在该上限!");
                else
                    LimitContext.Instance.CacheUpperLimitList.Add(limit);
            }
            if (amount < limit.UpperLlimit)
            {
                if (amount < limit.TotalBetAmount) //小于总下注金额
                {
                    throw new PromptException(string.Format("金额不能小于{0}!", limit.TotalBetAmount));
                }
            }
            limit.UpperLlimit = amount;
            DaUpperLimit.Update(limit);
            LimitContext.Instance.UpdateUpperLimitToDB(limit);
        }
        public void StartAcceptBet(int limitId, User user)
        {
            if (user.Role != SystemRole.Company) return;
            DaUpperLimit.UpdateBetStatus(limitId, false);
            LimitContext.Instance.UpdateUpperLimit(limitId);
        }
        public void StopAcceptBet(int limitId, User user)
        {
            if (user.Role != SystemRole.Company) return;
            DaUpperLimit.UpdateBetStatus(limitId, true);
            LimitContext.Instance.UpdateUpperLimit(limitId);
        }
        public BetUpperLimit GetLimit(int limitId)
        {
            var limit = LimitContext.Instance.CacheUpperLimitList.Find(it => it.LimitId == limitId);
            if (limit == null)
                limit = DaUpperLimit.GetUpperLimit(limitId);
            return limit;
        }
        /// <summary>
        /// 获取今日投注上限限制.
        /// </summary>
        /// <returns></returns>
        public IList<BetUpperLimit> ListTodayUpperLimit()
        {
            return DaUpperLimit.ListUpperLimitByDate(DateTime.Today);
        }
        public IList<BetUpperLimit> ListUpperLimitByCondition(DateTime date, string num, int companyId, int gameplayway)
        {
            return DaUpperLimit.ListUpperLimitByCondition(date, num, companyId, gameplayway);
        }
    }
}
